package coil3.gif

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import coil3.ImageLoader
import coil3.compose.AsyncImage

/**
 * Android 平台的 UniversalAsyncImage 实现
 * 
 * Android 端使用 Coil 的 AsyncImage，并配置 GIF 解码器支持动画
 */
@Composable
actual fun UniversalAsyncImage(
    url: String,
    contentDescription: String?,
    modifier: Modifier,
    onLoading: (() -> Unit)?,
    onSuccess: (() -> Unit)?,
    onError: ((Throwable?) -> Unit)?
) {
    val context = LocalContext.current
    val imageLoader = remember {
        ImageLoader.Builder(context)
            .components {
                // 注册 GIF 解码器
                add(GifDecoder.Factory())
                add(AnimatedImageDecoder.Factory())
            }
            .build()
    }
    AsyncImage(
        model = url,
        contentDescription = contentDescription,
        imageLoader = imageLoader,
        modifier = modifier,
        onLoading = { onLoading?.invoke() },
        onSuccess = { onSuccess?.invoke() },
        onError = { error -> onError?.invoke(error.result.throwable) }
    )
}
